﻿package mortal.game.view.task
{
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class TaskLinkProcessor extends Object
    {
        private var _lastTimerId:int = -1;

        public function TaskLinkProcessor()
        {
            return;
        }// end function

        public function processFlyBootTarget(param1:GLinkTextData) : void
        {
            var _loc_4:SPassPoint = null;
            var _loc_5:int = 0;
            var _loc_2:* = param1.data as TaskInfo;
            if (param1.type == GLinkTextData.pvp)
            {
                if (Game.mapInfo.mapId == param1.mapId)
                {
                    MsgManager.showRollTipsMsg(Language.getString(21237));
                    return;
                }
            }
            if (param1.subType == GLinkTextData.subType_TaskAutoFight)
            {
                GameController.taskImpl.guideToAutoFight(GameController.taskImpl.getAutoFightPoint(param1.mapId, false), true);
                return;
            }
            if (_loc_2 != null && _loc_2.stask.group == ETaskGroup._ETaskGroupGuild && Cache.instance.guild.hasGuild)
            {
                if (!Cache.instance.guild.isInGuild)
                {
                    AIManager.cancelAll();
                    _loc_4 = new SPassPoint();
                    _loc_4.type = PassAI.EnterGuildType;
                    _loc_4.mapId = Cache.instance.guild.selfGuildInfo.baseInfo.guildId;
                    AIManager.addPassToMap(_loc_4);
                    AIManager.addCallback(this.enteredGuildAndFly, param1);
                    AIManager.start();
                    return;
                }
                if (Cache.instance.guild.isInSelfGuild)
                {
                }
                else
                {
                    MsgManager.showRollTipsMsg(Language.getString(21238));
                    return;
                }
            }
            if (param1.type == GLinkTextData.boss)
            {
                if (CollectAI.collectingBoss != null && CollectAI.collectingBoss.code == param1.value1)
                {
                    return;
                }
            }
            var _loc_3:* = new SPassTo();
            _loc_3.mapId = param1.mapId;
            _loc_3.toPoint = new SPoint();
            _loc_3.toPoint.x = param1.x;
            _loc_3.toPoint.y = param1.y;
            AIManager.cancelAll();
            if (GameMapUtil.isCopyMap(_loc_3.mapId))
            {
                _loc_5 = 0;
                if (param1.type == GLinkTextData.boss)
                {
                    _loc_5 = 300;
                    if (GameMapUtil.isCopyMap(_loc_3.mapId))
                    {
                        _loc_5 = 10000000;
                    }
                }
                AIManager.addFlyBootToCopy(Game.mapInfo.mapId, _loc_3.mapId, new Point(_loc_3.toPoint.x, _loc_3.toPoint.y), _loc_5, this.flyAndMoveToed, [param1]);
                if (!AIManager.isWorking)
                {
                    AIManager.start();
                }
                return;
            }
            else if (!AIManager.addFlyBoot(_loc_3))
            {
                return;
            }
            AIManager.addCallback(this.flyedToCallback, param1);
            this.startAI();
            return;
        }// end function

        private function flyedToCallback(param1:GLinkTextData) : void
        {
            this.flyAndMoveToed(param1);
            return;
        }// end function

        private function startAI() : void
        {
            if (!AIManager.isWorking)
            {
                AIManager.start();
                AIManager.singleClickNoStop = true;
            }
            return;
        }// end function

        private function enteredGuildAndFly(param1:GLinkTextData) : void
        {
            var _loc_2:* = new SPassTo();
            _loc_2.mapId = param1.mapId;
            _loc_2.toPoint = new SPoint();
            _loc_2.toPoint.x = param1.x;
            _loc_2.toPoint.y = param1.y;
            AIManager.addFlyBoot(_loc_2);
            this.flyAndMoveToed(param1);
            return;
        }// end function

        private function flyAndMoveToed(param1:GLinkTextData) : void
        {
            var _loc_3:String = null;
            var _loc_2:* = param1.data as TaskInfo;
            switch(param1.type)
            {
                case GLinkTextData.boss:
                {
                    this.toAddFightBoss([param1, 10]);
                    break;
                }
                case GLinkTextData.Point:
                {
                    AIManager.addCallback(this.gotoPointFinished, param1);
                    break;
                }
                case GLinkTextData.npc:
                {
                    AIManager.addCallback(this.preShowNpc, [param1, 50]);
                    break;
                }
                case GLinkTextData.self:
                {
                    AIManager.cancelAll();
                    GameController.npc.npcDialog.show();
                    _loc_3 = Cache.instance.npc.getTaskTalk(param1.data as TaskInfo);
                    if (_loc_3 == null || _loc_3 == "")
                    {
                    }
                    GameController.npc.npcDialog.showSelf(param1.data as TaskInfo, _loc_3);
                    return;
                }
                default:
                {
                    break;
                }
            }
            if (!AIManager.isWorking)
            {
                AIManager.start();
            }
            return;
        }// end function

        public function processClickTarget(param1:GLinkTextData) : void
        {
            var _loc_2:TaskInfo = null;
            var _loc_3:String = null;
            var _loc_4:SPassPoint = null;
            var _loc_5:int = 0;
            var _loc_6:SceneInfo = null;
            var _loc_7:SPassPoint = null;
            var _loc_8:SPassTo = null;
            var _loc_9:Point = null;
            _loc_2 = param1.data as TaskInfo;
            GuideEffect.isShowPath = true;
            switch(param1.type)
            {
                case GLinkTextData.pvp:
                {
                    if (Game.mapInfo.mapId == param1.mapId)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(21239));
                        return;
                    }
                    AIManager.cancelAll();
                    if (AIManager.addMoveToOtherMap(Game.mapInfo.mapId, param1.mapId, null))
                    {
                        this.startAI();
                    }
                    break;
                }
                case GLinkTextData.boss:
                {
                    if (CollectAI.collectingBoss != null)
                    {
                        if (CollectAI.collectingBoss != null && param1.value1 == CollectAI.collectingBoss.code)
                        {
                            return;
                        }
                    }
                    if (_loc_2 != null && _loc_2.stask.group == ETaskGroup._ETaskGroupGuild && Cache.instance.guild.hasGuild && !Cache.instance.guild.isInGuild)
                    {
                        AIManager.cancelAll();
                        _loc_4 = new SPassPoint();
                        _loc_4.type = PassAI.EnterGuildType;
                        _loc_4.mapId = Cache.instance.guild.selfGuildInfo.baseInfo.guildId;
                        AIManager.addPassToMap(_loc_4);
                        AIManager.addCallback(this.toAddFightBoss, [param1, 300]);
                        AIManager.start();
                    }
                    else
                    {
                        AIManager.cancelAll();
                        _loc_5 = 1000;
                        if (GameMapUtil.isCopyMap(param1.mapId))
                        {
                            _loc_5 = 3000000;
                        }
                        if (!AIManager.addMoveToOtherMap(Game.mapInfo.mapId, param1.mapId, new Point(param1.x, param1.y), _loc_5, this.addAutoFightBoss, [param1, _loc_5]))
                        {
                            return;
                        }
                        this.startAI();
                    }
                    break;
                }
                case GLinkTextData.Point:
                {
                    AIManager.cancelAll();
                    if (!AIManager.addMoveToOtherMap(Game.mapInfo.mapId, param1.mapId, new Point(param1.x, param1.y), 60, this.addToPointFinishTask, [param1]))
                    {
                        return;
                    }
                    this.startAI();
                    break;
                }
                case GLinkTextData.self:
                {
                    AIManager.cancelAll();
                    GameController.npc.npcDialog.show();
                    _loc_3 = Cache.instance.npc.getTaskTalk(_loc_2);
                    if (_loc_3 == null || _loc_3 == "")
                    {
                    }
                    GameController.npc.npcDialog.showSelf(_loc_2, _loc_3);
                    break;
                }
                case GLinkTextData.escort:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.EscortCopyOpenSelectTaskWindow));
                    break;
                }
                case GLinkTextData.npc:
                {
                    if (_loc_2 != null && (_loc_2.stask.group == ETaskGroup._ETaskGroupGuild || param1.value1 == 3001000))
                    {
                        if (!Cache.instance.guild.hasGuild)
                        {
                            MsgManager.showRollTipsMsg(Language.getString(21240));
                        }
                        else if (!Cache.instance.guild.isInGuild)
                        {
                            AIManager.cancelAll();
                            _loc_4 = new SPassPoint();
                            _loc_4.type = PassAI.EnterGuildType;
                            _loc_4.mapId = Cache.instance.guild.selfGuildInfo.baseInfo.guildId;
                            AIManager.addPassToMap(_loc_4);
                            AIManager.addDelayAI(200);
                            AIManager.addCallback(this.clickToShowNpcPanel, param1);
                            AIManager.start();
                        }
                        else if (!Cache.instance.guild.isInSelfGuild)
                        {
                            AIManager.cancelAll();
                            if (!Cache.instance.guild.isInSelfGuild && Game.sceneInfo != null)
                            {
                                _loc_6 = Game.sceneInfo;
                                _loc_7 = _loc_6.passInfos[0] as SPassPoint;
                                _loc_8 = _loc_7.passTo[0] as SPassTo;
                                _loc_9 = new Point();
                                _loc_9.x = _loc_7.point.x;
                                _loc_9.y = _loc_7.point.y;
                                AIManager.addMoveTo(_loc_9, 0);
                                AIManager.addPassToMap(_loc_7);
                                AIManager.addDelayAI(200);
                            }
                            _loc_4 = new SPassPoint();
                            _loc_4.type = PassAI.EnterGuildType;
                            _loc_4.mapId = Cache.instance.guild.selfGuildInfo.baseInfo.guildId;
                            AIManager.addPassToMap(_loc_4);
                            AIManager.addCallback(this.clickToShowNpcPanel, param1);
                            AIManager.start();
                        }
                        else if (Cache.instance.guild.isInSelfGuild)
                        {
                            this.clickToShowNpcPanel(param1);
                        }
                    }
                    else
                    {
                        this.clickToShowNpcPanel(param1);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function addAutoFightBoss(param1:GLinkTextData, param2:int) : void
        {
            AIManager.addCallback(this.toAddFightBoss, [param1, param2]);
            this.startAI();
            return;
        }// end function

        private function toAddFightBoss(param1:Array) : void
        {
            var _loc_6:Dictionary = null;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:GLinkTextData = null;
            var _loc_10:Dictionary = null;
            var _loc_11:IEntity = null;
            var _loc_12:int = 0;
            var _loc_13:MonsterPlayer = null;
            var _loc_2:* = param1[0];
            var _loc_3:* = param1[1];
            var _loc_4:* = _loc_2.data as TaskInfo;
            var _loc_5:int = -1;
            if (_loc_4 != null)
            {
                _loc_5 = _loc_4.stask.code;
                _loc_7 = Cache.instance.task.getLinkTextData(_loc_5);
                if (_loc_2.isAutoFightType)
                {
                    AIManager.addAutoFight(-1, null, _loc_4.stask.code);
                    return;
                }
                if (_loc_2.isNewCollectType())
                {
                    _loc_6 = new Dictionary();
                    _loc_7 = _loc_2.subType.split(";");
                    _loc_8 = 0;
                    while (_loc_8 < _loc_7.length)
                    {
                        
                        _loc_6[parseInt(_loc_7[_loc_8])] = true;
                        _loc_8++;
                    }
                }
                else if (_loc_4.stask.guide != TaskGuideType.Guide_ClickNotOnlyBoss && !CopyUtil.isShowBossKilledMap)
                {
                    _loc_6 = new Dictionary();
                    if (_loc_7 != null)
                    {
                        _loc_7 = _loc_7[_loc_4.curStep];
                        if (_loc_7 != null)
                        {
                            for each (_loc_9 in _loc_7)
                            {
                                
                                _loc_6[_loc_9.value1] = true;
                            }
                        }
                    }
                }
            }
            if (_loc_6 != null && _loc_3 >= 200 && (!CopyUtil.isInDefenseCopy || CopyUtil.isInGuideDefenseCopy))
            {
                _loc_10 = ThingUtil.entityUtil.entitysMap.allEntitys;
                for each (_loc_11 in _loc_10)
                {
                    
                    _loc_13 = _loc_11 as MonsterPlayer;
                    if (_loc_13 == null)
                    {
                        continue;
                    }
                    if (_loc_6[_loc_13.bossInfo.code] == true)
                    {
                        if (GeomUtil.calcDistance(RolePlayer.instance.x2d, RolePlayer.instance.y2d, _loc_13.x2d, _loc_13.y2d) > 550)
                        {
                            AIManager.addMoveTo(new Point(_loc_13.x2d, _loc_13.y2d), 300);
                        }
                        AIManager.addAutoFight(-1, _loc_6, _loc_5);
                        return;
                    }
                }
                if (_loc_3 > 10000)
                {
                    _loc_3 = 8000;
                }
                _loc_12 = 200;
                if (_loc_3 > 4000)
                {
                    _loc_12 = 500;
                }
                AIManager.addMoveToOtherMap(Game.mapInfo.mapId, _loc_2.mapId, new Point(_loc_2.x, _loc_2.y), _loc_3 - _loc_12, this.addAutoFightBoss, [_loc_2, _loc_3 - _loc_12]);
            }
            else if (CopyUtil.isInDefenseCopy)
            {
                AIManager.addAutoFight(-1, null, _loc_5);
            }
            else
            {
                AIManager.addAutoFight(-1, _loc_6, _loc_5);
            }
            return;
        }// end function

        private function gotoPointFinished(param1:GLinkTextData) : void
        {
            var _loc_2:* = param1.data as TaskInfo;
            var _loc_3:* = _loc_2.stask.processMap[_loc_2.curStep];
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = _loc_3[0];
            if (_loc_4 == null)
            {
                return;
            }
            switch(_loc_4.type)
            {
                case ETaskProcess._ETaskProcessTreasure:
                {
                    GameProxy.task.digTreasure();
                    break;
                }
                case ETaskProcess._ETaskProcessExplore:
                {
                    GameProxy.task.explore();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function addToPointFinishTask(param1:GLinkTextData) : void
        {
            AIManager.addDelayAI(300);
            AIManager.addCallback(this.gotoPointFinished, param1);
            return;
        }// end function

        private function clickToShowNpcPanel(param1:GLinkTextData) : void
        {
            var _loc_3:NPCInfo = null;
            var _loc_4:Point = null;
            var _loc_2:* = SceneConfig.instance.getSceneInfoByNpcId(param1.value1);
            if (_loc_2 != null)
            {
                _loc_3 = _loc_2.getNpcInfo(param1.value1);
            }
            if (_loc_3 == null)
            {
                return;
            }
            AIManager.cancelAll();
            if (param1.value2 == 0)
            {
                if (Game.mapInfo == null)
                {
                    return;
                }
                _loc_4 = GameMapUtil.getPixelPoint(_loc_3.snpc.point.x / GameMapUtil.tileWidth, _loc_3.snpc.point.y / GameMapUtil.tileHeight);
                if (!AIManager.addMoveToOtherMap(Game.mapInfo.mapId, param1.mapId, _loc_4, GameConst.NpcInDistance, this.preShowNpc, [[param1, 50]]))
                {
                    return;
                }
            }
            else
            {
                GameController.npc.showNpcFuncPanel(_loc_3);
            }
            this.startAI();
            return;
        }// end function

        private function preShowNpc(param1:Array) : void
        {
            if (this._lastTimerId > 0)
            {
                clearTimeout(this._lastTimerId);
                this._lastTimerId = -1;
            }
            AIManager.addCallback(this.addShowNpc, param1);
            return;
        }// end function

        private function addShowNpc(param1:Array) : void
        {
            var _loc_2:* = param1[0];
            var _loc_3:* = _loc_2.data as TaskInfo;
            var _loc_4:* = param1[1];
            param1[1] = _loc_4 - 1;
            var _loc_5:* = ThingUtil.npcUtil.getNpc(_loc_2.value1);
            if (_loc_5 == null)
            {
                if (_loc_4 > 0)
                {
                    this._lastTimerId = setTimeout(this.addShowNpc, 60, param1);
                }
                return;
            }
            this._lastTimerId = -1;
            if (!AIManager.isWorking)
            {
                AIManager.cancelAll();
            }
            if (_loc_3 != null && (_loc_3.stask.autoGet && _loc_3.isCanget() || _loc_3.stask.autoEnd && _loc_3.isComplete()))
            {
            }
            else
            {
                AIManager.addClickNpcAI(_loc_2.value1);
            }
            if (!AIManager.isWorking)
            {
                AIManager.start();
            }
            return;
        }// end function

    }
}
